

$(function(){

// 显示隐藏函数
function toggle(init){   

var state=(init.show=='true')?'block':'none';

$(init.obj).on(init.event,function(){
	$(init.target).css('display',state)
})
}

// 运动函数
function startMove(obj,json,endFn){
	clearInterval(obj.timer);		
		obj.timer = setInterval(function(){			
			var bBtn = true;		
			for(var attr in json){				
				var iCur = 0;	
				if(attr == 'opacity'){
					if(Math.round(parseFloat(getStyle(obj,attr))*100)==0){
					iCur = Math.round(parseFloat(getStyle(obj,attr))*100);}
					else{
						iCur = Math.round(parseFloat(getStyle(obj,attr))*100) || 100;}	
				}
				else{
					iCur = parseInt(getStyle(obj,attr)) || 0;}	
				var iSpeed = (json[attr] - iCur)/8;
			iSpeed = iSpeed >0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
				if(iCur!=json[attr]){
					bBtn = false;
				}	
				if(attr == 'opacity'){
					obj.style.filter = 'alpha(opacity=' +(iCur + iSpeed)+ ')';
					obj.style.opacity = (iCur + iSpeed)/100;		
				}
				else{
					obj.style[attr] = iCur + iSpeed + 'px';
				}		
			}			
			if(bBtn){
				clearInterval(obj.timer);				
				if(endFn){
					endFn.call(obj);
				}
			}			
		},30);	
	}		
	function getStyle(obj,attr){
		if(obj.currentStyle){
			return obj.currentStyle[attr];
		}
		else{
			return getComputedStyle(obj,false)[attr];
		}
	}

// 初始化
(function(){

toggle({
	obj:".top span",
	event:"click",
	target:".top",
	show:'false'})
// nav导航
for(var i=1;i<4;i++){
		toggle({
	obj:"nav .drop"+i,
	event:"mouseover",
	target:" .drop"+i+" .son",
	show:'true'})
toggle({
	obj:"nav .drop"+i,
	event:"mouseout",
	target:" .drop"+i+" .son",
	show:'false'})
}
// 购物车
toggle({
	obj:".header_shop",
	event:"mouseover",
	target:".header_shop div",
	show:'true'})	
toggle({
	obj:".header_shop",
	event:"mouseout",
	target:".header_shop div",
	show:'false'})	

// banner模块

for(var i=0;i<15;i++){
	toggle({
	obj:".banner .li"+i,
	event:"mouseover",
	target:".banner .li"+i+" div",
	show:'true'})	
toggle({
	obj:".banner .li"+i,
	event:"mouseout",
	target:".banner .li"+i+" div",
	show:'false'})
}
})()



function slide(obj,init){
	clearInterval(obj.timer);
	var inow=0;
	var wrap=obj.children[0];
	var w=wrap.children[0].offsetWidth;
	var len=wrap.children.length
	// 复制一份
	wrap.style.width=w*len*2+'px';
	wrap.innerHTML+=wrap.innerHTML
// 定时器
if(init.time){
obj.timer=setInterval(move,init.time)}
// 触摸停止
obj.onmouseover=function(){
	init.right.style.opacity='1';
	init.left.style.opacity='1'
	clearInterval(obj.timer);
}
obj.onmouseout=function(){
		init.right.style.opacity='0';
	init.left.style.opacity='0'
	if(init.time){
	obj.timer=setInterval(move,init.time)}
}

function move(){
inow++;
if(init.navbar){
	for(var i=0;i<init.navbar.children.length;i++){
		init.navbar.children[i].className=''
	}
	init.navbar.children[inow%len].className="active"}
startMove(wrap,{left:-inow*w},function(){
	if(inow==(len*2-1)){
		wrap.style.left=-(len-1)*w+'px';
		inow=len-1;	}
})
}
// 左右按钮
init.right.onclick=function(){
	if(inow==(len*2-1)){
		wrap.style.left=-(len-1)*w+'px';
		inow=len-1;
	}
	move()
}
init.left.onclick=function(){
	if(inow==0){
		wrap.style.left=-len*w+'px';
		inow=len}
	inow-=2
	move()}

}
// 轮播
(function(){
var a=document.getElementById('banner1')
var btn1=document.querySelector('#banner1 .btn1')
var btn2=document.querySelector('#banner1 .btn2')
var bar=document.querySelector('#banner1 ul')
a.children[0].style.width=790*3+'px';
slide(a,{
	time:'2000',
	navbar:bar,
	left:btn1,
	right:btn2
})
})();

// 京东秒杀

// 做一个移动端页面
(function   (){
	var obj=document.getElementById('seckill');
var btn1=document.querySelector('#seckill .left');
var btn2=document.querySelector('#seckill .right')
slide(obj,{
	left:btn1,
	right:btn2
})
})();


//导航条
(function(){
		  var oul =document.querySelector('#navbarleft ul');
		  var oli=document.querySelectorAll('#navbarleft ul li');
		  var timer=null;
		 	  
		  for(var i=0;i<oli.length;i++){
			  oli[i].index=i;
			  
			  oli[i].onclick=function(){
				  
				  for(var i=0;i<oli.length;i++){
					  oli[i].className="";
					  }
			 this.className='active'
			 if(this.index=="10"){
					  scroll(0)
					  return;
					  }
			var obj=document.getElementById('item'+this.index);			
			scroll(obj.offsetTop)
			  }
			  
			  }
		  
		function scroll(target){
			clearInterval(timer)
			timer=setInterval(function(){
				 if(document.body.scrollTop>target){
					 document.body.scrollTop-=20;
					 }else{document.body.scrollTop+=20;}
				if(Math.abs(document.body.scrollTop-target)<=10){
					
					clearInterval(timer)
					} },1)
			}  
			
		   window.onscroll=function(){
			  var item1=document.getElementById('item0');
			  var scrollbar=document.getElementById('navbarleft')
			  if(item0.getBoundingClientRect().top<=200){
				  scrollbar.style.opacity="1"
				  }else{scrollbar.style.opacity="0"}
				 for(var i=0;i<oli.length;i++){
					 if(i==10){return;}
					 var obj=document.getElementById('item'+i);
					
					 if(Math.abs(document.body.scrollTop-obj.offsetTop)<=50){
						  for(var j=0;j<oli.length;j++){
							 oli[j].className="";
							 }
						 oli[i].className='active';
				 }
					 	 }	
			  }
			  size();
		  window.onresize=size
		  
		 function size(){
			var head=document.querySelector('header');
			var bar =document.getElementById('navbarleft');
			if(head.offsetLeft<=80){
				
					bar.style.left=10+'px';
				}else{
				bar.style.left=head.offsetLeft-75+'px';
					}
			
			
			
			
			 }
		  
		  
		  
		  
		  
		  })();





})





























